#include <pybind11/embed.h> // 包含嵌入解释器的头文件
#include <pybind11/stl.h>   // 支持 vector、map 等自动转换
#include <iostream>         // 必须包含这个头文件才能使用 std::cout 和 std::endl

namespace py = pybind11;

int main() {
    // 启动 Python 解释器（自动在析构时关闭）
    py::scoped_interpreter guard{};

    // 将当前路径加入 sys.path，确保能导入模块
    py::module sys = py::module::import("sys");
    sys.attr("path").attr("append")("."); // 添加当前目录到模块搜索路径

    // 导入 Python 模块
    py::module my_module = py::module::import("my_python_module");


    // 调用 Python 函数 add(10, 20)
    int add = my_module.attr("add")(10, 20).cast<int>();

    std::cout << "Result from Python: " << add << std::endl;
	
	
	
	// test more parameter and return value
	{
		 // 获取函数
		py::object process_data = my_module.attr("process_data");

		// 构造参数
		std::string name = "Alice";
		int age = 23;
		std::vector<double> scores = {85.5, 90.0, 78.0, 65.5};

		// 调用函数
		py::object result = process_data(name, age, scores);

		// 输出结果
		std::cout << "Result from Python:\n" << py::str(result).cast<std::string>() << std::endl;

		// 提取具体字段
		double average = result["average"].cast<double>();
		double total = result["total"].cast<double>();
		std::vector<bool> passed = result["passed"].cast<std::vector<bool>>();

		std::cout << "Average score: " << average << std::endl;
		std::cout << "Total score: " << total << std::endl;
		std::cout << "Passed: ";
		for (bool b : passed)
			std::cout << (b ? "Yes" : "No") << " ";
		std::cout << std::endl;
	}
	
	
	// test class
	{
		 // 获取类类型
		py::object MyProcessorClass = my_module.attr("MyProcessor");

		// 创建实例 (调用构造函数)
		std::string name = "DataProcessor";
		py::object processor = MyProcessorClass(name); // 相当于 MyProcessor("DataProcessor")

		// 准备参数
		int age = 23;
		std::vector<double> scores = {85.5, 90.0, 78.0, 65.5};

		// 调用对象方法
		py::object result = processor.attr("process")(age, scores);

		// 输出结果
		std::cout << "Result from Python:\n" << py::str(result).cast<std::string>() << std::endl;

		// 提取具体字段
		double average = result["average"].cast<double>();
		double total = result["total"].cast<double>();
		std::vector<bool> passed = result["passed"].cast<std::vector<bool>>();

		std::cout << "Average score: " << average << std::endl;
		std::cout << "Total score: " << total << std::endl;
		std::cout << "Passed: ";
		for (bool b : passed)
			std::cout << (b ? "Yes" : "No") << " ";
		std::cout << std::endl;
	}

    return 0;
}